Table Migration using artisan

  • STEP

    create product migration file

    
                      php artisan make:migration create_products_table
                      

    this will generate a file in database\migrations folder.

    open the file 'create_products_table' and add required table fields

    
                      public function up()
                      {
                          Schema::create('contacts', function (Blueprint $table) 
                          {
                                  $table->id();
                                  $table->string('name');
                                  $table->string('mobile_no');
                                  $table->boolean('status');
                                  $table->timestamps();
                          });
                      }
    
                      

    Run The Migration

    
                      php artisan migrate
                    

    Rollback The Migration

    
                    php artisan migrate:rollback
                    

    Adding/Updating Columns in Table

    For e.g. Updating the column name we should run command like

    
                    php artisan make:migration update_name_column_in_products_table
                    

    open the file 'update_name_column_in_products_table' and add the fields

    
                    Schema::table('products', function (Blueprint $table) {
                      $table->index('email');
                   });
                  
    For renaming a index you can use renameIndex() for e.g.
    
                    Schema::table('products', function (Blueprint $table) {
                        $table->renameIndex('email', 'mobile_no');
                   });
                  
    For dropping a index you can use dropIndex() for e.g.
    
                    Schema::table('products', function (Blueprint $table) {
                      $table->dropIndex('email');
                   });